<?php
namespace app\index\controller;
use think\Db;
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 
class Order extends Base
{
    //创建订单
    public function add()
    {
        //订单是否存在
        if(empty($_POST['order_value'])){
            echo json_encode(array('提交失败'));
            exit;
        }
        //获取订单值
        $data=$_POST['order_value'];
        //生成随机订单号
        $data['order_name']=rand_order_num();
        $data['create_time']=time();
        $order=model('order');
        $orderdata=$order->insert($data);
        if($orderdata){
            $result=array(
                'status'=>0,
                'info' =>$data
            );
        }else{
            $result=array(
                'status'=>0
            );  
        }
        //返回
        echo json_encode($result);
    }
    //订单详情
    public function detail(){
        //订单是否存在
        if(empty($_GET['order_name'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
          exit;
        }
        $ordername=$_GET['order_name'];
        $order=model('order');
        $data=$order->where(array('order_name'=>$ordername))->find();
        if($data){
            $result=array(
                'status'=>0,
                'info'=>$data
            );
        }else{
            $result=array(
                'status'=>1
            );
        }
        //返回
        echo json_encode($result);
    }
    //订单数据
    public function index(){
        if(empty($_GET['username'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
          exit;
        }
        $order=model('order');
        $current = $_GET['current'];
        $showItem= $_GET['showItem'];
        $shownum=intval(($current-1))*intval($showItem);
        $username=$_GET['username'];
        $data=$order->field('buy_person,create_time,buy_person,order_name,total_price,pay,address,id')->where(array('buy_person'=>$username))->limit($shownum,$showItem)->select();
        $allcount=$order->field('buy_person,create_time,buy_person,order_name,total_price,pay,address,id')->where(array('buy_person'=>$username))->count();
        if($data){
            $result=array(
                'status'=>0,
                'info'=> $data,
                'allcount' => $allcount
            );
        }else{
            $result=array(
                'status'=>1
            );
        }
        //返回
        echo json_encode($result);
    }   
    //订单状态更改
    public function status_change(){
        if(empty($_GET['id']) || empty($_GET['status'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
          exit;
        }
        $id=$_GET['id'];
        $status=$_GET['status'];
        $order=model('order');
        $result =$order->save(['pay'=>$status],['id'=>$id]);
        if($result){
            $res=array(
                'status'=>0,
            );
        }else{
            $res=array(
                'status'=>1
            );
        }
        //返回
        echo json_encode($res);
    }
    //查询订单中所有包含的商品
    public function order_goods(){
        //参数缺少返回
        if(empty($_GET['id'])){
          echo json_encode(array('status'=>1,'info'=>'缺少参数'));
          exit;
        }

        $order=model('order');
        $id=$_GET['id'];
        //获取订单中食物ID字段
        $orderdata=$order->field('food_id,pay')->where(array('id'=>$id))->find();

        //错误返回
        if(!$orderdata){
            echo json_encode(array('status'=>1));
            exit;
        }
        //根据字段提取出ID
        $foods_list = explode(',',$orderdata['food_id']);
        $food=model('food');
        $foods_data=array();
        //获取食物信息,并存放在数组中
        foreach ($foods_list as $k => $v) {
            $data = $food->field('title,price_old,summary,img_url,id')->where(array('id'=>$v))->find();
            $data['pay']=$orderdata['pay']; 
            array_push($foods_data,$data);
        }
        if($foods_data){
            $res=array(
                'status' => 0,
                'info' =>$foods_data
            );
        }else{
            $res=array(
                'status' => 1,
            );   
        }
        echo json_encode($res);
    }
}
